package com.kky.pp.popup

import android.Manifest
import android.app.Activity
import android.os.Build
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import com.kky.pp.R
import com.kky.pp.dot.DotLogEventName
import com.kky.pp.dot.DotLogUtil
import com.kky.pp.util.PermissionUtils
import com.custom.base.config.BaseConfig
import io.rong.imkit.SpName
import razerdp.basepopup.BasePopupWindow

/**
 * 权限请求
 */
class NotificationRequestPopup(var ctx: Activity, var nickName:String, var listener: OnListener) :  BasePopupWindow(ctx) {
    init {
        setContentView(R.layout.popup_notification_request)
        initView()
        showAnimation = AnimationUtils.loadAnimation(ctx, R.anim.dialog_anim_enter)
        dismissAnimation=AnimationUtils.loadAnimation(ctx, R.anim.dialog_anim_exit)
        setOutSideDismiss(true)
    }

    private fun initView() {
        if (nickName.isNullOrEmpty()){
            BaseConfig.getInstance.setBoolean(SpName.firstShowNotification,true)
        }else{
            BaseConfig.getInstance.setBoolean(SpName.firstShowNotification+nickName,true)
        }
        val dialogTitle = findViewById<TextView>(R.id.txtRequestNotificationTitle)
        val dialogConfirm = findViewById<TextView>(R.id.txtRequestNotificationDone)

        if (nickName!= ""){
            dialogTitle.text="${ctx.getString(R.string.want_to_stay_in_touch_with)} $nickName ?"
//            HttpRequest.commonNotify(501,"")
            DotLogUtil.setEventName(DotLogEventName.NOTIFICATION_PAGE_WITH_EFFECTIVE_CHAT).commit()

        }else{
//            HttpRequest.commonNotify(500,"")
            DotLogUtil.setEventName(DotLogEventName.FIRST_NOTIFICATION_PAGE).commit()
        }
        val dialogCancel = findViewById<ImageView>(R.id.imgViewRequestNotificationCancel)
        dialogCancel.setOnClickListener {
            dismiss()
        }
        dialogConfirm.setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                PermissionUtils.requestPermission(ctx,
                    {
                        listener.onGrantedListener()
//                        HttpRequest.commonNotify(503,if (nickName!= "")"501" else "500")
                        DotLogUtil.setEventName(DotLogEventName.NOTIFICATION_PAGE_GRANTED).setRemark(if (nickName!= "")"501" else "500").commit()

                    }, {
                        listener.onDeniedListener()
//                        HttpRequest.commonNotify(503,if (nickName!= "")"501" else "500")
                        DotLogUtil.setEventName(DotLogEventName.NOTIFICATION_PAGE_GRANTED).setRemark(if (nickName!= "")"501" else "500").commit()
                    },Manifest.permission.POST_NOTIFICATIONS)
            }
            dismiss()
        }
        setOutSideDismiss(true)
    }

    interface OnListener{
        fun onGrantedListener()
        fun onDeniedListener()
    }
}